﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Hartley.ApplicationLogic;
using Hartley.DTOs;

namespace WebApplication1
{
    public partial class viewExecutiveCommitteeById : System.Web.UI.Page
    {
        MemberAL memberAL;
        string queryString;
        Guid committeeId;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                queryString = Request.QueryString.ToString();
                committeeId = Guid.Empty;

                bool convertableToGuid = Guid.TryParse(queryString, out committeeId);
                if (convertableToGuid)
                {
                    CreateDynamicTable(committeeId);

                }
                else
                {
                    //Wrong Committee Id
                }

            }
        }


        private void CreateDynamicTable(Guid committeeId)
        {
            PlaceHolder6.Controls.Clear();


            memberAL = new MemberAL();
            

            List<CommitteeDTO> committeeDTO = memberAL.GetCommitteeById(committeeId);
            List<CommitteeMemberDTO> committeeMemberDTO = memberAL.GetAllMemberOfCommitteeByCommitteeId(committeeId).ToList();

            // Create a Table and set its properties 
            Table tbl = new Table();
            tbl.Width = new Unit("100%");
            tbl.BorderStyle = BorderStyle.Solid;
            // Add the table to the placeholder control
            PlaceHolder6.Controls.Add(tbl);
            //Label1.Text = "Detailed view of an Executive Committee";
            //PlaceHolder6.Controls.Add(Label1);
            Table subtbl = new Table();
            subtbl.Width = new Unit("50%");
            subtbl.BorderStyle = BorderStyle.Solid;

            Label lblCommitteeName = new Label();
            lblCommitteeName.Text = "Committee Name";

            Label lblCommitteeFunctionedFrom = new Label();
            lblCommitteeFunctionedFrom.Text = "Functioned From";

            Label lblCommitteeFunctionedTill = new Label();
            lblCommitteeFunctionedTill.Text = "Functioned Till";

            Label lblCommitteeDesc = new Label();
            lblCommitteeDesc.Text = "Description";

            //Label lblCommitteeContribution = new Label();
            //lblCommitteeContribution.Text = "Contribution";


            List<Label> allHeaderLabels = new List<Label>();

            allHeaderLabels.Add(lblCommitteeName);
            allHeaderLabels.Add(lblCommitteeFunctionedFrom);
            allHeaderLabels.Add(lblCommitteeFunctionedTill);
             allHeaderLabels.Add(lblCommitteeDesc);
            // allHeaderLabels.Add(lblCommitteeContribution);

            int i = 0;
            foreach (Label lblHeader in allHeaderLabels)
            {


                TableRow trHeader = new TableRow();
                trHeader.Width = new Unit("50%");

                TableCell tcHeader = new TableCell();
                TableCell tcHeaderValue = new TableCell();

                Label lblValue = new Label();
                if (i % 2 == 0)
                {
                    tcHeader.BackColor = System.Drawing.Color.Indigo;
                    tcHeader.ForeColor = System.Drawing.Color.White;

                    tcHeaderValue.BackColor = System.Drawing.Color.Indigo;
                    tcHeaderValue.ForeColor = System.Drawing.Color.White;

                }
                else
                {
                    tcHeader.BackColor = System.Drawing.Color.White;
                    tcHeader.ForeColor = System.Drawing.Color.Indigo;

                    tcHeaderValue.BackColor = System.Drawing.Color.White;
                    tcHeaderValue.ForeColor = System.Drawing.Color.Indigo;
                }

                if (lblHeader.Text == "Committee Name")
                {
                    lblValue.Text = committeeDTO[0].Title;
                }

                else if (lblHeader.Text == "Functioned From")
                {
                    lblValue.Text = committeeDTO[0].StartDate.ToShortDateString();
                }

                else if (lblHeader.Text == "Functioned Till")
                {
                    lblValue.Text = committeeDTO[0].EndDate.ToShortDateString();
                }
                 else if (lblHeader.Text == "Description")
                {
                    lblValue.Text = committeeDTO[0].Description;
                }
                /* else if (lblHeader.Text == "Contribution")
                {
                    lblValue.Text = committeeDTO[0].Contribution;
                }*/
                tcHeader.Controls.Add(lblHeader);
                tcHeaderValue.Controls.Add(lblValue);
                trHeader.Cells.Add(tcHeader);
                trHeader.Cells.Add(tcHeaderValue);


                tbl.Rows.Add(trHeader);
                i++;
            }

            int tblRows = committeeMemberDTO.Count;

            int j = i;
            // Now iterate through the table and add your controls 
            foreach (CommitteeMemberDTO committeeMember  in committeeMemberDTO)
            {
                List<MemberDTO> memberDTO = memberAL.GetMemberById(committeeMemberDTO[j-i].MemberId);
                TableRow trBody = new TableRow();
                trBody.Width = new Unit("50%");

                TableCell tcBodyDesignation = new TableCell();
                TableCell tcBodyMemberName = new TableCell();

                Label lblDesignation = new Label();
                HyperLink hlMemberName = new HyperLink();
                if (j % 2 == 0)
                {
                    tcBodyDesignation.BackColor = System.Drawing.Color.Indigo;
                    tcBodyDesignation.ForeColor = System.Drawing.Color.White;

                    tcBodyMemberName.BackColor = System.Drawing.Color.Indigo;
                    tcBodyMemberName.ForeColor = System.Drawing.Color.White;

                }
                else
                {
                    tcBodyDesignation.BackColor = System.Drawing.Color.White;
                    tcBodyDesignation.ForeColor = System.Drawing.Color.Indigo;

                    tcBodyMemberName.BackColor = System.Drawing.Color.White;
                    tcBodyMemberName.ForeColor = System.Drawing.Color.Indigo;
                }

                int noOfPatron = committeeMemberDTO.Where(a => a.Position == "Patron").ToList().Count;
                int noOfVPatrons = committeeMemberDTO.Where(a => a.Position == "Vice Patron").ToList().Count;
                int noOfImmPresident = committeeMemberDTO.Where(a => a.Position == "Immediate Past president").ToList().Count;
                int noOfPresident = committeeMemberDTO.Where(a => a.Position == "President").ToList().Count;
                int noOfVicePresident = committeeMemberDTO.Where(a => a.Position == "Vice President").ToList().Count;
                int noOfSecy = committeeMemberDTO.Where(a => a.Position == "Secretary").ToList().Count;
                int noOfAsstSecy = committeeMemberDTO.Where(a => a.Position == "Asst.Secretary").ToList().Count;
                int noOfTreasurer = committeeMemberDTO.Where(a => a.Position == "Treasurer").ToList().Count;
                int noOfAsstTreasurer = committeeMemberDTO.Where(a => a.Position == "Asst.Treasurer").ToList().Count;
                int noOfEditor = committeeMemberDTO.Where(a => a.Position == "Editor").ToList().Count;
                int noOfAuditor = committeeMemberDTO.Where(a => a.Position == "Auditor").ToList().Count;
                int noOfCommMember = committeeMemberDTO.Where(a => a.Position == "Committee Member").ToList().Count;

                if ((committeeMemberDTO[j-i].Position == "Vice Patron")) 
                {
                    if ((j - i - noOfPatron) == noOfVPatrons / 2 )
                    {
                        lblDesignation.Text = committeeMemberDTO[j - i].Position + "(s)";
                    }
                   
                    hlMemberName.Text = memberDTO[0].FullName;
                    hlMemberName.NavigateUrl = "viewAllRolesOfCommMember.aspx?" + memberDTO[0].MemberId;

                    tcBodyDesignation.Controls.Add(lblDesignation);
                    tcBodyMemberName.Controls.Add(hlMemberName);
                    
                    
                }

                else if ((committeeMemberDTO[j - i].Position == "Vice President"))
                {
                    if ((j - i - noOfPatron - noOfVPatrons - noOfImmPresident) == noOfVicePresident / 2 + 1)
                    {
                        lblDesignation.Text = committeeMemberDTO[j - i].Position + "(s)";
                    }

                    hlMemberName.Text = memberDTO[0].FullName;
                    hlMemberName.NavigateUrl = "viewAllRolesOfCommMember.aspx?" + memberDTO[0].MemberId;

                    tcBodyDesignation.Controls.Add(lblDesignation);
                    tcBodyMemberName.Controls.Add(hlMemberName);


                }

                else if ((committeeMemberDTO[j - i].Position == "Committee Member"))
                {
                    if ((j - i - noOfPatron - noOfVPatrons - noOfImmPresident - noOfVicePresident - noOfSecy - noOfAsstSecy - noOfTreasurer - noOfAsstTreasurer - noOfEditor - noOfAuditor) == committeeMemberDTO.Where(a => a.Position == "Committee Member").ToList().Count / 2 + 1)
                    {
                        lblDesignation.Text = committeeMemberDTO[j - i].Position + "(s)";
                    }

                    hlMemberName.Text = memberDTO[0].FullName;
                    hlMemberName.NavigateUrl = "viewAllRolesOfCommMember.aspx?" + memberDTO[0].MemberId;

                    tcBodyDesignation.Controls.Add(lblDesignation);
                    tcBodyMemberName.Controls.Add(hlMemberName);


                }
                else
                {

                    lblDesignation.Text = committeeMemberDTO[j-i].Position;
                    hlMemberName.Text = memberDTO[0].FullName;
                    hlMemberName.NavigateUrl = "viewAllRolesOfCommMember.aspx?" + memberDTO[0].MemberId;


                    tcBodyDesignation.Controls.Add(lblDesignation);
                    tcBodyMemberName.Controls.Add(hlMemberName);
                }

                trBody.Cells.Add(tcBodyDesignation);
                trBody.Cells.Add(tcBodyMemberName);



                tbl.Rows.Add(trBody);
                j++;
            }

            // Add the TableRow to the Table

            //tbl.Rows.Add(trBody);
            // }

            // This parameter helps determine in the LoadViewState event,
            // whether to recreate the dynamic controls or not

            ViewState["dynamictable"] = true;
            hlViewAllSubCommitteesOfCommittee.NavigateUrl = "viewAllSubCommittee.aspx?" + committeeId;
            hlViewAllMeetingOfCommittee.NavigateUrl = "viewAllMeeting.aspx?" + committeeId;
            hlViewAllMinutesOfCommittee.NavigateUrl = "viewAllMintes.aspx?" + committeeId;
        }

        }
    }

